package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 设备分类数据库操作
 */

public class DeviceCategoryDao {
    private Context mContext;
    private Dao<DeviceCategory, Integer> deviceCategoryDaoOpe;
    private DatabaseHelper helper;

    public DeviceCategoryDao(Context context) {
        mContext = context;
//        mContext = context.getApplicationContext();
        try {
            helper = DatabaseHelper.getHelper(mContext);
            deviceCategoryDaoOpe = helper.getDao(DeviceCategory.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(DeviceCategory deviceCategory) {
        try {
            deviceCategoryDaoOpe.create(deviceCategory);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public int updateDeviceCategory(DeviceCategory deviceCategory) {
        try {
            deviceCategory.setChecked(false);
            return deviceCategoryDaoOpe.update(deviceCategory);
            //    LogHelper.i("updateLightDeviceCategorynumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public DeviceCategory get(int id) {
        try {
            return deviceCategoryDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public DeviceCategory getByUuid(String uuid) {
        List<DeviceCategory> deviceCategoryList = null;
        try {
            deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (deviceCategoryList != null && deviceCategoryList.size() > 0) {
                return deviceCategoryList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    /**
     * @return
     */
    public List<DeviceCategory> getAllDeviceCategory() {
        List<DeviceCategory> deviceCategoryList = new ArrayList<>();
        try {
            deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceCategoryList;
    }

    public DeviceCategory getById(String groupId) {
        List<DeviceCategory> deviceCategoryList = null;
        try {
            deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).query();
            if (deviceCategoryList != null && deviceCategoryList.size() > 0) {
                return deviceCategoryList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<DeviceCategory> getFamilyDeviceCategorys(String familyId) {
        List<DeviceCategory> deviceCategoryList = new ArrayList<>();
        try {
            deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().orderBy("category",true).orderBy("groupId",true).where().eq ("familyId", familyId).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceCategoryList;
    }

    //获取当前家庭的照明设备
    public List<DeviceCategory> getFamilyLighting(String familyId) {
        List<DeviceCategory> deviceCategoryList = new ArrayList<>();
        try {
            deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().orderBy("category",true).orderBy("groupId",true).where().eq ("familyId", familyId).and().eq("category",1).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceCategoryList;
    }


    //获取当前家庭的小家电设备---查询条件是当前家庭码中category大于1的时候为小家电设备
    public List<DeviceCategory> getFamilyHEA(String familyId) {
        List<DeviceCategory> deviceCategoryList = new ArrayList<>();
        try {
            deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().orderBy("category",true).orderBy("groupId",true).where().eq ("familyId", familyId).and().gt("category",1).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceCategoryList;
    }


    public List<DeviceCategory> getHEAIfModelNull() {
        List<DeviceCategory> deviceCategoryList = new ArrayList<>();
        try {
            deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().where().isNull("model").and().gt("category",1).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceCategoryList;
    }


    public int delete(DeviceCategory deviceCategory) {
        try {
            return deviceCategoryDaoOpe.delete(deviceCategory);
            //    LogHelper.i("deleLightDeviceCategorynumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = deviceCategoryDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<DeviceCategory> deviceCategoryList) {
        try {
            int number = deviceCategoryDaoOpe.delete(deviceCategoryList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public DeviceCategory getDeviceCategoryByName(String familyId, String name) {
        try {
            List<DeviceCategory> deviceCategoryList = deviceCategoryDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).query();
            if (deviceCategoryList != null && deviceCategoryList.size() > 0)
                return deviceCategoryList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



    public DeviceCategory getFamilyHEAByDeviceid(String familyId,String deviceId) {
        try {
            List<DeviceCategory> heaList = deviceCategoryDaoOpe.queryBuilder().where().eq("familyId", familyId).and().eq("deviceId", deviceId).query();
            int total = heaList.size();
            if (total > 0) {
                return heaList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



    public DeviceCategory getFamilyBallLightByGroupId(String familyId,String groupId) {
        try {
            List<DeviceCategory> heaList = deviceCategoryDaoOpe.queryBuilder().where().eq("familyId", familyId).and().eq("groupId", groupId).and().eq("category", 1).query();
            int total = heaList.size();
            if (total > 0) {
                return heaList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
